Skip to content

Fix SCF loop type inference with bounded widening - #730

Open
nathanieltornow wants to merge 1 commit into
QuEraComputing:mainfrom
nathanieltornow:fix-scf-loop-type-inference
Open

Fix SCF loop type inference with bounded widening#730
nathanieltornow wants to merge 1 commit into
QuEraComputing:mainfrom
nathanieltornow:fix-scf-loop-type-inference

Conversation

@nathanieltornow

Copy link
Copy Markdown

Fixes #512. Supports #728.

Infer scf.For loop-carried types to a stable invariant before publishing body types. Widen changing literal parameters while retaining stable information. Preserves explicit element types for empty ILists.

This prevents IList unrolling from using lengths that are valid only during the first iteration. For example:

from typing import Any
from kirin.dialects import ilist
from kirin.passes import TypeInfer
from kirin.prelude import structural_no_opt
from kirin.rewrite import Walk

@structural_no_opt
def identity(x: int) -> int:
    return x

@structural_no_opt
def grow(n: int) -> ilist.IList[int, Any]:
    xs = ilist.IList([])
    for _ in range(n):
        xs = xs + [4]
        xs = ilist.map(identity, xs)
    return xs

print("Before passes:", list(grow(3)))

TypeInfer(grow.dialects, no_raise=False)(grow)
print("After TypeInfer:", list(grow(3)))

Walk(ilist.rewrite.Unroll()).rewrite(grow.code)
print("After Unroll:", list(grow(3)))

Before the fix:

Before passes: [4, 4, 4]
After TypeInfer: [4, 4, 4]
After Unroll: [4]

After the fix:

Before passes: [4, 4, 4]
After TypeInfer: [4, 4, 4]
After Unroll: [4, 4, 4]

Infer loop-carried types to a stable invariant and publish the converged body types. Widen changing literal parameters and bound structural growth, raising an interpreter error if inference cannot establish an invariant. Preserve explicit element types for empty ILists.

Cover incorrect map unrolling on growing lists, branch-dependent and dependent loop updates, nested loops, retained precision, repeated inference, and bounded fallback.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect type inference in scf.For

1 participant